package com.cityfood.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfCommentMapper;
import com.cityfood.pojo.CfComment;
import com.cityfood.pojo.CfCommentExample;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfCommentExample.Criteria;
import com.cityfood.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CfCommentMapper commentMapper;

	@Override
	public List<CfComment> getComment() {
		CfCommentExample example = new CfCommentExample();
		Criteria criteria = example.createCriteria();
		List<CfComment> list = commentMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<CfComment> getCommentByProductId(Integer productId) {
		CfCommentExample example = new CfCommentExample();
		Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		List<CfComment> list = commentMapper.selectByExample(example);
		return list;
	}

	@Override
	public PageInfo<CfComment> getCommentByProductIdAndPage(Integer productId, Integer pageNum, Integer pageSize) {
		// 设置分页的页数和行数
		PageHelper.startPage(pageNum, pageSize);

		CfCommentExample example = new CfCommentExample();
		Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		List<CfComment> list = commentMapper.selectByExample(example);

		PageInfo<CfComment> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public PageInfo<CfComment> getCommentByProductIdAndPage(Integer productId, List<Byte> number, Integer isPic,
			Integer pageNum, Integer pageSize) {

		// 设置分页的页数和行数
		PageHelper.startPage(pageNum, pageSize);

		// List<CfComment> list =
		// commentMapper.selectCommentOrderByTime(productId,number);

		CfCommentExample example = new CfCommentExample();
		example.setOrderByClause("`create_time` desc");

		Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		criteria.andCommentNumberIn(number);
		if (isPic != 0) {
			criteria.andCommentImageIsNotNull();
		}
		List<CfComment> list = commentMapper.selectByExample(example);
		PageInfo<CfComment> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public int deleteComment(CfComment comment) {
		comment.setIsdisplay((byte) 0);
		int i = commentMapper.updateByPrimaryKeySelective(comment);
		return i;
	}

	@Override
	public int statCommentsStar(int productId, int starNum) {
		CfCommentExample example = new CfCommentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCommentNumberEqualTo((byte) starNum);
		criteria.andProductIdEqualTo(productId);
		int count = commentMapper.countByExample(example);
		return count;
	}

	@Override
	public int statCommentsHavePic(int productId) {
		CfCommentExample example = new CfCommentExample();
		Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		criteria.andCommentImageIsNotNull();
		int count = commentMapper.countByExample(example);
		return count;
	}

	@Override
	public int insertComment(CfComment comment) {
		int i = commentMapper.insertSelective(comment);
		return i;
	}

	@Override
	public PageInfo<CfComment> getCommentByCommentMapAndPage(Map<String, Object> commentMap, Integer pageNum,
			Integer pageSize) {
		// 设置分页的页数和行数
		PageHelper.startPage(pageNum, pageSize);

		// List<CfComment> list =
		// commentMapper.selectCommentOrderByTime(productId,number);

		CfCommentExample example = new CfCommentExample();
		example.setOrderByClause("`create_time` desc");

		Criteria criteria = example.createCriteria();

		if (commentMap.containsKey("productIds")) {
			criteria.andProductIdIn((List)commentMap.get("productIds"));
		}
		if (commentMap.containsKey("userIds")) {
			criteria.andUserIdIn((List)commentMap.get("userIds"));
		}

		if (commentMap.containsKey("starNumber")) {
			criteria.andCommentNumberEqualTo(Byte.parseByte(commentMap.get("starNumber").toString()));
		}

		List<CfComment> list = commentMapper.selectByExample(example);
		PageInfo<CfComment> info = new PageInfo<>(list);
		return info;
	}

}
